package visiopuce.ui.view;

import java.text.Collator;
import java.util.List;
import java.util.Locale;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.part.ViewPart;

import visiopuce.objets.Analyse;
import visiopuce.service.AnalyseServiceImpl;
import visiopuce.ui.listener.DoubleCliqueListeTravailListener;
import visiopuce.ui.providers.ListeADNTableLabelProvider;

public class ListeADNView extends ViewPart {

	public static final String ID = "visiopuce.listeADNView";
	private TableViewer tableViewer;
	private Composite c;

	public ListeADNView() {
	}

	public void createPartControl(Composite parent) {

		final ScrolledComposite sc = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);

		c = new Composite(sc, SWT.NONE);
		c.setLayout(new GridLayout(1, true));
		c.setSize(400, 400);
		sc.setContent(c);
		sc.setExpandHorizontal(true);
		sc.setExpandVertical(true);
		sc.getVerticalBar().setIncrement(10);
		sc.getHorizontalBar().setIncrement(10);
		sc.setMinSize(c.computeSize(1000, 1000));

		c.setLayout(new GridLayout(1, true));

//		final Text input = new Text(c, SWT.SINGLE | SWT.BORDER);
//		final Button searchBtn = new Button(c, SWT.BORDER | SWT.PUSH);
//		searchBtn.setText("Rechercher");
//		searchBtn.addSelectionListener(new SelectionAdapter() {
//			public void widgetSelected(SelectionEvent e) {
//				TableItem[] tia = tableViewer.getTable().getItems();
//				for (int i = 0; i < tia.length; i++) {
//					if (tia[i].getText(2).equals(input.getText())) {
//						tia[i].setBackground(new Color(Display.getCurrent(), 127, 178, 127));
//					}
//
//				}
//			}
//		});

		tableViewer = new TableViewer(c, SWT.FULL_SELECTION | SWT.VIRTUAL);
		createColumns();
		Table table = tableViewer.getTable();
		GridData gd = new GridData(SWT.LEFT, SWT.FILL, false, true);
		gd.minimumHeight = 150;// Math.min(150, analyses.size() + 1 * 10);

		table.setLayoutData(gd);

		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		tableViewer.setUseHashlookup(true);
		tableViewer.setContentProvider(ArrayContentProvider.getInstance());
		// tableViewer.setContentProvider(new LazyContentProvider(tableViewer));
		tableViewer.addDoubleClickListener(new DoubleCliqueListeTravailListener());
		// tableViewer.setItemCount(30);
		// tableViewer[i].refresh();
		tableViewer.getTable().pack();
	}

	public void refresh(int annee) {
		List<Analyse> analyses = AnalyseServiceImpl.getInstance().getListeADN(annee);
		tableViewer.setInput(analyses);
		tableViewer.getTable().pack();
	}

	// private void createTable(String secteur, int i) {
	//
	//
	// }

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	private void createColumns() {

		String[] titles = new String[] { "Nom", "Prénom", "Date de naissance", "Prélèvement", "Date de rendu", "Date de prélèvement", "Tissus", "Prescripteur", "Analyses", "Indication", "N° de boite", "Date d'extraction ADN", "commentaire", "Feuille de travail", "Sexe" };
		int[] bounds = { 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80 };

		TableViewerColumn column1 = createTableViewerColumn(titles[0], bounds[0], 0);
		column1.getColumn().addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event event) {
				// sort column 1
				TableItem[] items = tableViewer.getTable().getItems();
				Collator collator = Collator.getInstance(Locale.getDefault());
				for (int i = 1; i < items.length; i++) {
					String value1 = items[i].getText(0);
					for (int j = 0; j < i; j++) {
						String value2 = items[j].getText(0);
						if (collator.compare(value1, value2) < 0) {
							String[] values = { items[i].getText(0), items[i].getText(1) };
							items[i].dispose();
							TableItem item = new TableItem(tableViewer.getTable(), SWT.NONE, j);
							item.setText(values);
							items = tableViewer.getTable().getItems();
							break;
						}
					}
				}

			}
		});
		TableViewerColumn column2 = createTableViewerColumn(titles[1], bounds[1], 1);
		TableViewerColumn column3 = createTableViewerColumn(titles[2], bounds[2], 2);
		TableViewerColumn column4 = createTableViewerColumn(titles[3], bounds[3], 3);
		TableViewerColumn column5 = createTableViewerColumn(titles[4], bounds[4], 4);
		TableViewerColumn column6 = createTableViewerColumn(titles[5], bounds[5], 5);
		TableViewerColumn column7 = createTableViewerColumn(titles[6], bounds[6], 6);
		TableViewerColumn column8 = createTableViewerColumn(titles[7], bounds[7], 7);
		TableViewerColumn column9 = createTableViewerColumn(titles[8], bounds[8], 8);
		TableViewerColumn column10 = createTableViewerColumn(titles[9], bounds[9], 9);
		TableViewerColumn column11 = createTableViewerColumn(titles[10], bounds[10], 10);
		TableViewerColumn column12 = createTableViewerColumn(titles[11], bounds[11], 11);
		TableViewerColumn column13 = createTableViewerColumn(titles[12], bounds[12], 12);
		TableViewerColumn column14 = createTableViewerColumn(titles[13], bounds[13], 13);

	}

	private TableViewerColumn createTableViewerColumn(String header, int width, int idx) {
		TableViewerColumn column = new TableViewerColumn(tableViewer, SWT.LEFT, idx);
		column.getColumn().setText(header);
		column.getColumn().setWidth(width);
		column.getColumn().setResizable(true);
		column.getColumn().setMoveable(true);
		column.setLabelProvider(new ListeADNTableLabelProvider(idx));

		return column;
	}

}